package com.skymxc.wandroidtodo.page.base;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.skymxc.wandroidtodo.mvp.IView;
import com.skymxc.wandroidtodo.page.login.LoginActivity;

/**
 * <p>
 *
 * </p>
 *
 * @author 孟祥超
 * <p>
 * date: 2021/12/1  7:02 上午
 */
public abstract class BaseActivity<M extends BasePresenter> extends AppCompatActivity implements IView {
    private ProgressDialog mWaitDialog;
    private AlertDialog mErrorDialog;

    protected M mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        mPresenter = initPresenter();

        if (null != mPresenter) {
            mPresenter.attachView(this);
        }
        initData(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        if (null != mPresenter) {
            mPresenter.detachView(this);
        }
        super.onDestroy();
    }

    protected void initView() {
    }

    protected abstract M initPresenter();

    protected void initData(Bundle savedInstanceState) {
    }

    @Override
    public void showProgress(String msg) {
        if (null == mWaitDialog) {
            mWaitDialog = new ProgressDialog(this);
        }
        mWaitDialog.setMessage(msg);
        mWaitDialog.show();
    }

    @Override
    public void hideProgress() {
        if (null != mWaitDialog && mWaitDialog.isShowing()) {
            mWaitDialog.dismiss();
            Log.i("BaseActivity.java", "hideProgress: "+mWaitDialog.isShowing());
        }else{
            Log.i("BaseActivity.java", "hideProgress: mWaitDialog 没有显示");
        }
    }

    @Override
    public void showError(String msg, int errCode) {
        hideProgress();
        if (null == mErrorDialog) {
            mErrorDialog = new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setNegativeButton("确定", null)
                    .create();
        }
        mErrorDialog.setMessage("(" + errCode + ")" + msg);
        mErrorDialog.show();
    }

    public void toLogin(){
        showConfirm("登陆超时，请重新登陆", false, new DialogListener() {
            @Override
            public void onConfirm() {
                Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

            @Override
            public void onCancel() {

            }
        });
    }

    public void showConfirm(String msg) {
        showConfirm(msg, true, null);
    }

    public void showConfirm(String msg, boolean cancelable, DialogListener listener) {
        showConfirm(msg, cancelable, false, listener);
    }

    public void showConfirm(String msg, boolean cancelable, boolean showCancel, DialogListener listener) {
        hideProgress();
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage(msg)
                .setCancelable(cancelable)
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (null != listener) listener.onConfirm();
                    }
                });
        if (showCancel) {
            builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if (null != listener) listener.onCancel();
                }
            });
        }
        builder.create().show();
    }

    public interface DialogListener {
        void onConfirm();

        void onCancel();
    }
}
